package com.wlj.util.mp;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

@Component
public class MyMetaObjectHandler implements MetaObjectHandler {

    //定义插入的时候如何填充
    @Override
    public void insertFill(MetaObject metaObject) {
        boolean hasSetter = metaObject.hasSetter("createTime");
        //有这个字段的时候再填充
        if(hasSetter){
            setInsertFieldValByName("createTime", LocalDateTime.now(),metaObject);
        }

    }

    //定义更新的时候如何填充
    @Override
    public void updateFill(MetaObject metaObject) {
        Object updateTime = getFieldValByName("updateTime", metaObject);
        //事先没有设置过updateTime再填充
        if(updateTime==null){
            setUpdateFieldValByName("updateTime",LocalDateTime.now(),metaObject);
        }

    }
}
